package viroyal.com.base.activity.micro_service;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

import viroyal.com.base.R;
import viroyal.com.base.activity.exam.ExamDelegate;
import viroyal.com.base.activity.exam.ExamModel;
import viroyal.com.base.fragment.MultiplexFragment;
import viroyal.com.dev.NFCMonitorBaseActivity;

public class MicroServiceActivity extends NFCMonitorBaseActivity<MicroServiceDelegate, MicroServiceModel> implements ViewPager.OnPageChangeListener, View.OnTouchListener {

  private List<Fragment> mFragmentLists;
  // 记录开始手指点击的位置,和滑动的X距离
  private int StartX, SlipX;
  // 当前的页面
  private int currentItemNum = 0;

  @Override
  protected void initView(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.initView(savedInstanceState);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewPager home_page = findViewById(R.id.vp_home_page);
    mFragmentLists = new ArrayList<>();
    mFragmentLists.add(MultiplexFragment.newInstance("https://service.vcampus.cn/smart_serves/bookreader_large/index.html#/", "书香校园"));
    mFragmentLists.add(MultiplexFragment.newInstance("http://wxy.iyuyun.net/mobile_school_h5/public/app/historytoday/#/", "历史上的今天"));
    mFragmentLists.add(MultiplexFragment.newInstance("https://service.vcampus.cn/smart_serves/notice_announcement/index.html#/School_getlost", "失物招领"));
    //自定义一个适配器
    home_page.setOffscreenPageLimit(1);
    ViewPagerAdapter mAdater = new ViewPagerAdapter(getSupportFragmentManager());
    home_page.setAdapter(mAdater);
    home_page.addOnPageChangeListener(this);
    home_page.setOnTouchListener(this);
  }

  @Override
  protected Class<MicroServiceDelegate> getDelegateClass() {
    return MicroServiceDelegate.class;
  }

  @Override
  protected Class<MicroServiceModel> getModelClass() {
    return MicroServiceModel.class;
  }

  @Override
  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

  }

  @Override
  public void onPageSelected(int position) {
    currentItemNum= position;
  }

  @Override
  public void onPageScrollStateChanged(int state) {

  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
    // 一定要spuer，否则事件打住,不会在向下调用了
    super.dispatchTouchEvent(ev);
    switch (ev.getAction()) {
      // 记录用户手指点击的位置
      case MotionEvent.ACTION_DOWN:
        StartX = (int) ev.getX();
        Log.i("info", "StartX = " + StartX);
        break;
    }
    return true;// return false,继续向下传递，return true;拦截,不向下传递
  }

  @Override
  public boolean onTouch(View view, MotionEvent event) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        StartX = (int) event.getX();
        Log.i("info", "StartX11111 = " + StartX);
        break;
      case MotionEvent.ACTION_MOVE:
        // 获取滑动时候的X距离
        SlipX = (int) event.getX();
        Log.i("info", "1= " + SlipX);
        if (currentItemNum == 0 && SlipX - StartX > 100) {
          // 在第一个页面,手势向右滑动,finish当前页面
          finish();
          // 设置切换切换动画
          overridePendingTransition(R.anim.slide_left_in,
              R.anim.slide_right_out);
        }
        break;

      default:
        break;
    }
    return false;
  }

  //适配器
  class ViewPagerAdapter extends FragmentPagerAdapter {
    ViewPagerAdapter(FragmentManager fm) {
      super(fm);
    }

    //获取Fragment
    @Override
    public Fragment getItem(int position) {
      return mFragmentLists.get(position);
    }

    //总共有mFragmentLists.size()个需要显示
    @Override
    public int getCount() {
      return mFragmentLists.size();
    }
  }

}
